package com.aaa.controller.sds;

import com.aaa.entity.LayData;
import com.aaa.entity.MyPage;
import com.aaa.entity.sds.TSds;
import com.aaa.service.sds.TSdsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @version 1.8.0_181
 * @Author ZXJ
 * @Date 2022/10/25 20:24
 * @注释
 */
@Controller
public class TSdsController {
    @Autowired
    private TSdsService tSdsService;

    //查询所有
//    @RequestMapping("/queryAllSds")
//    @ResponseBody
//    public LayData queryAllSds(MyPage myPage, TSds tSds){
//        PageInfo pageInfo =tSdsService.queryAllSds(myPage,tSds);
//        System.out.println("pageInfo = " + pageInfo);
//        return new LayData(pageInfo);
//    }
    //添加
    @RequestMapping("/insertSds")
    @ResponseBody
    public LayData insertSds(TSds tSds){
        boolean insert = tSdsService.insertSds(tSds);
        System.out.println("insert = " + insert);
        return new LayData(insert);
    }

    //根据id删除
    @RequestMapping("/deleteSds")
    @ResponseBody
    public LayData deleteSds(Integer id,Integer sdsno){
        System.out.println("单删前台id"+id);
        System.out.println("单删前台sdsno = " + sdsno);
        boolean delete = tSdsService.deleteSds(id,sdsno);
        System.out.println("delete = " + delete);
        return new LayData(delete);
    }

    /**
     * 批量删除
     * @param id
     * @return
     */
    @RequestMapping("/delbath")
    @ResponseBody
    public LayData delbath(@RequestParam Integer[] id, @RequestParam Integer[] sdsno){
        System.out.println("前台传递：id = " + id);
        System.out.println("前台传递：sdsno = " + sdsno);
        boolean delsdsbath = tSdsService.delsdsbath(id,sdsno);
        System.out.println("delsdsbath = " + delsdsbath);
        return new LayData(delsdsbath);
    }


    @RequestMapping("/updSds")
    @ResponseBody
    public LayData updSds(TSds tSds){
        boolean update = tSdsService.updateSds(tSds);
        System.out.println("update = " + update);
        return new LayData(update);
    }


    /**
     * 题号id唯一校验
     * @param tSds
     * @return
     */
    @RequestMapping("/querySdsname")
    @ResponseBody
    public LayData querySdsno(TSds tSds){
        boolean b = tSdsService.querySdsname(tSds);
        if (b){
           // return success ("可以用");

            return new LayData("可用");

        }
        return new LayData("题目重复不可用！！！");
    }



    // 批量导入数据
    @Resource
    private TSdsService fileService;
    @RequestMapping(value = "/import")
    @ResponseBody
    public LayData storageService(MultipartFile file) {
        fileService.dealFile(file);
        System.out.println("file = " + file);
        return new LayData(true);
    }

    /**
     * 做题
     * @param myPage
     * @param request
     * @return
     */
    @GetMapping("/index")
    public String getIndexAllSub(MyPage myPage, HttpServletRequest request){
        System.out.println("============================");
        myPage.setLimit(20);
        myPage.setPage(1);
        PageInfo all = fileService.getAll(myPage);
        request.setAttribute("allsub",all);

        return "sds/index";

    }

    /**
     * 模糊
     * @param myPage
     * @param tSds
     * @return
     */
    @RequestMapping("querysdsAll")
    @ResponseBody
public LayData querysdsAll(MyPage myPage,TSds tSds){
    PageInfo pageInfo = tSdsService.querysdsAll(myPage, tSds);
    System.out.println("pageInfo = " + pageInfo.toString());
    return new LayData(pageInfo);
}


    /**
     * 题目编号失焦事件
     * @param sdsno
     * @return
     */
    @RequestMapping("queryBySdsno/{sdsno}")
    @ResponseBody
    public LayData queryBySdsno(@PathVariable Integer sdsno){
        boolean b = tSdsService.queryBySdsno(sdsno);
        return new LayData(b);
    }





}
